package com.mgkj.controller.WMS;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mgkj.common.result.Result;
import com.mgkj.dto.QueryMoReceiptD;
import com.mgkj.dto.ScanToSignForReceiptDTO;
import com.mgkj.mapper.DeliveryNoticeMapper;
import com.mgkj.mapper.PickingMapper;
import com.mgkj.vo.BarCodeDetailVo;
import com.mgkj.vo.IssueReceiptDVo;
import com.mgkj.vo.MoReceiptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "工单入库签收相关接口")
@RequestMapping("/MoSign")
public class MoSignController {

    @Resource
    private PickingMapper pickingMapper;

    @Autowired
    private DeliveryNoticeMapper deliveryNoticeMapper;

    @PostMapping("/getList")
    @ApiOperation("获取工单入库签收列表")
    public Result<PageInfo<MoReceiptVo>> getList(@RequestBody QueryMoReceiptD dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MoReceiptVo> list = pickingMapper.queryMoReceipt(dto);
        PageInfo<MoReceiptVo> pageInfo = new PageInfo<>(list);
        return Result.ok(pageInfo);
    }

    @PostMapping("/scanToSignForReceipt")
    @ApiOperation("扫码签收")
    public Result scanToSignForReceipt(@RequestBody ScanToSignForReceiptDTO dto) {
        BarCodeDetailVo barCodeDetail = deliveryNoticeMapper.getBarCodeDetailByBarCode(dto.getBarcode());
        if (barCodeDetail == null) {
            return Result.fail().message("条码不存在");
        }
        try {
            for (String id : dto.getIssueReceiptIdList()) {
                MoReceiptVo moReceiptVo = pickingMapper.queryMoReceiptD(id);
                double qty = moReceiptVo.getAcceptedQty() >= barCodeDetail.getQty() ? barCodeDetail.getQty() : moReceiptVo.getAcceptedQty();
                pickingMapper.updateMoReceiptD(id, qty);
            }
        } catch (Exception e) {
            return Result.fail().message("签收失败" + e.getMessage());
        }
        return Result.ok().message("签收成功");
    }

}
